import 'package:flutter/material.dart';
import 'package:check_app/widgets/Picture.dart';
import 'package:check_app/dao/subject_dao.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:check_app/model/subjectInfo.dart';

class SubjectDetailPage extends StatefulWidget {
  final int id;
  final String subName;
  const SubjectDetailPage({
    Key key,
    this.id,
    this.subName,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState() => new _SubjectDetailPageState();
}

class _SubjectDetailPageState extends State<SubjectDetailPage> {
  bool _loading = false;
  int id;
  int taskId;
  String taskCode;
  String customerName;
  String subName;
  String code;
  String checkTime;
  String verdictType;
  String verdictTypeText;
  List<SpecInfo> specList = [];
  List<String> picUrlList = [];
  List<Map> infoList = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<Null> getData() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await SubjectDao.fetchSubjectInfoById(
          '/subject/' + widget.id.toString());
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          id = result.data.id;
          taskId = result.data.taskId;
          taskCode = result.data.taskCode;
          customerName = result.data.customerName;
          subName = result.data.subName;
          code = result.data.code;
          checkTime = result.data.checkTime;
          verdictType = result.data.verdictType;
          verdictTypeText = result.data.verdictTypeText;
          specList = result.data.specList;
          if (specList.length == 0 ||
              specList[specList.length - 1].name != '检查结论') {
            specList
                .add(new SpecInfo(null, '检查结论', '', '', '', verdictTypeText));
          }
          picUrlList = result.data.picUrlList;
          infoList = [
            {
              'title': '所属任务',
              'key': taskCode,
            },
            {
              'title': '所属客户',
              'key': customerName,
            },
            {
              'title': '待检物编码',
              'key': code,
            },
            {
              'title': '待检物名称',
              'key': subName,
            },
          ];
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(subName ?? ''),
      ),
      body: Stack(
        children: <Widget>[
          SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
              child: Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 1,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Column(
                      children: getInfoList(infoList),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 1,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Column(
                      children: getSpecList(specList),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 8),
                    child: Column(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.only(top: 12, bottom: 12),
                          child: Text(
                            '图片',
                            style: TextStyle(
                                fontSize: 14,
                                height: 20 / 14,
                                color: Color.fromRGBO(150, 151, 154, 1)),
                          ),
                        ),
                        Picture(
                          count: 4,
                          uploadButton: false,
                          picList: picUrlList,
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 获取菜单项widget列表
  List<Widget> getInfoList(List info) {
    return info.map<Widget>((item) => createInfoItem(item)).toList();
  }

  // 生成单个菜单项
  Widget createInfoItem(item) {
    return Container(
      height: 40,
      child: Row(
        children: <Widget>[
          Container(
            alignment: Alignment.centerLeft,
            child: Text(
              item['title'],
              style: TextStyle(
                fontSize: 14,
                height: 20 / 14,
                color: Color.fromRGBO(150, 151, 154, 1),
              ),
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                item['key'],
                style: TextStyle(
                  fontSize: 14,
                  height: 20 / 14,
                  color: Color.fromRGBO(43, 44, 46, 1),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 获取规格widget列表
  List<Widget> getSpecList(List info) {
    return info.map<Widget>((item) => createSpecItem(item)).toList();
  }

  // 生成单个规格项
  Widget createSpecItem(item) {
    return Container(
      height: 40,
      child: Row(
        children: <Widget>[
          Container(
            alignment: Alignment.centerLeft,
            child: Text(
              item.name,
              style: TextStyle(
                fontSize: 14,
                height: 20 / 14,
                color: Color.fromRGBO(150, 151, 154, 1),
              ),
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                item.value,
                style: TextStyle(
                  fontSize: 14,
                  height: 20 / 14,
                  color: Color.fromRGBO(43, 44, 46, 1),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
